<?php

namespace Admin\Controller;

use Think\Controller;

class CallController extends Controller
{

    public  function test(){
        echo '123';
    }

    //来电去电的七陌对外推送
    public function call_record()
    {
        $CallNo = I('CallNo');//主叫号码
        $CalledNo = I('CalledNo');//被叫号码
        $CallSheetID = I('CallSheetID');//通话记录ID,CallSheetID 是这条通话记录再DB中的唯一id
        $CallType = I('CallType');//通话类型：dialout外呼通话,normal普通来电,transfer转接电话,dialTransfer外呼转接
        $Ring = I('Ring');//通话振铃时间（话务进入呼叫中心系统的时间）2015-06-12 20:14:50
        $RingingTimestamp = I('RingingTimestamp');//被叫振铃开始时间
        $Begin = I('Begin');//通话接通时间
        $End = I('End');//	通话结束时间
        $RecordFile = I('RecordFile');//通话录音文件名
        $FileServer = I('FileServer');//通过FileServer中指定的地址加上RecordFile的值可以获取录音
        $Agent = I('Agent');//	处理坐席的工号
        $State = I('State');//	接听状态：dealing（已接）,notDeal（振铃未接听）,leak（ivr放弃）,queueLeak（排队放弃）,blackList（黑名单）,voicemail（留言）
        $Province = I('Province');//目标号码的省,
        $District = I('District');//目标号码的市,
        $ActionID = I('ActionID');//接口调用ID,

        if ($CallType == 'dialout') {
            $Type = 1;
        } else if ($CallType == 'normal') {
            $Type = 2;
        } else if ($CallType == 'transfer') {
            $Type = 3;
        } else if ($CallType == 'dialTransfer') {
            $Type = 4;
        }
        $state_listen = '1';
        switch ($State) {
            case 'dealing':
                $state_listen = 1;
                break;
            case 'notDeal':
                $state_listen = 2;
                break;
            case 'leak':
                $state_listen = 3;
                break;
            case 'queueLeak':
                $state_listen = 4;
                break;
            case 'blackList':
                $state_listen = 5;
                break;
            case 'voicemail':
                $state_listen = 6;
                break;
        }

        $Ring = strtotime($Ring);
        $Begin = strtotime($Begin);
        $End = strtotime($End);
//        $db_user='USER';
//        $db_admin='KEFU';
        //坐席归属项目


        if($Agent=='8028' || $Agent=='8017' || $Agent=='8005' ){//马上花呗
            $type=2;
            $db_user='USER_BJ';
            $db_admin='KEFU_BJ';
        }else {
            $type=1;
            $db_user='USER';
            $db_admin='KEFU';
        }
        if($Agent=='8002' || $Agent=='8007' || $Agent=='8018'){

            $db_user='USER';
            $db_admin='KEFU';

            //来电
            $vid = M('user', '', $db_user)->where(['mobile' => $CallNo])->getfield('vid');
            //外呼
            if (empty($vid)){
                $vid = M('user', '', $db_user)->where(['mobile' => $CalledNo])->getfield('vid');
            }

            if (empty($vid)) {
                $vid = '1';
            }
            //坐席负责人
            $agent = $Agent . '@hyyjr';
            $admin = M('admin', '', $db_admin)->where(['agent' => $agent, 'status' => 1])->getField('name');
            $arr = array(
                'callno' => $CallNo,
                'calledno' => $CalledNo,
                'callsheetid' => $CallSheetID,
                'calltype' => $Type,
                'calltype_o' => $CallType,
                'ring' => $Ring,
                'ringingtimestamp' => $RingingTimestamp,
                'begin' => $Begin,
                'end' => $End,
                'agent' => $Agent,
                'state' => $state_listen,
                'voice_url' => $FileServer . '/' . $RecordFile,
                'listen_state' => $State,
                'province' => $Province,
                'district' => $District,
                'ctime' => time(),
                'vid' => $vid,
                'actionid' => $ActionID,
                'admin' => $admin,
                'type' => $type
            );

            $add = M('call_info', '', $db_admin)->add($arr);


            $db_user='USER_BJ';
            $db_admin='KEFU_BJ';

            //来电
            $vid = M('user', '', $db_user)->where(['mobile' => $CallNo])->getfield('vid');
            //外呼
            if (empty($vid)) {
                $vid = M('user', '', $db_user)->where(['mobile' => $CalledNo])->getfield('vid');
            }

            if (empty($vid)) {
                $vid = '1';
            }
            //坐席负责人
            $agent = $Agent . '@hyyjr';
            $admin = M('admin', '', $db_admin)->where(['agent' => $agent, 'status' => 1])->getField('name');
            $arr = array(
                'callno' => $CallNo,
                'calledno' => $CalledNo,
                'callsheetid' => $CallSheetID,
                'calltype' => $Type,
                'calltype_o' => $CallType,
                'ring' => $Ring,
                'ringingtimestamp' => $RingingTimestamp,
                'begin' => $Begin,
                'end' => $End,
                'agent' => $Agent,
                'state' => $state_listen,
                'voice_url' => $FileServer . '/' . $RecordFile,
                'listen_state' => $State,
                'province' => $Province,
                'district' => $District,
                'ctime' => time(),
                'vid' => $vid,
                'actionid' => $ActionID,
                'admin' => $admin,
                'type' => $type
            );
            $add = M('call_info', '', $db_admin)->add($arr);
            if ($add) {
                echo '200';
            }
            die;

        }


        //来电
        $vid = M('user', '', $db_user)->where(['mobile' => $CallNo])->getfield('vid');
        //外呼
        if (empty($vid)) {
            $vid = M('user', '', $db_user)->where(['mobile' => $CalledNo])->getfield('vid');
        }

        if (empty($vid)) {
            $vid = '1';
        }
        //坐席负责人
        $agent = $Agent . '@hyyjr';
        $admin = M('admin', '', $db_admin)->where(['agent' => $agent, 'status' => 1])->getField('name');
        $arr = array(
            'callno' => $CallNo,
            'calledno' => $CalledNo,
            'callsheetid' => $CallSheetID,
            'calltype' => $Type,
            'calltype_o' => $CallType,
            'ring' => $Ring,
            'ringingtimestamp' => $RingingTimestamp,
            'begin' => $Begin,
            'end' => $End,
            'agent' => $Agent,
            'state' => $state_listen,
            'voice_url' => $FileServer . '/' . $RecordFile,
            'listen_state' => $State,
            'province' => $Province,
            'district' => $District,
            'ctime' => time(),
            'vid' => $vid,
            'actionid' => $ActionID,
            'admin' => $admin,
            'type' => $type
        );

        $add = M('call_info', '', $db_admin)->add($arr);


        if ($add) {
            echo '200';
        }
    }

    //定时刷新用户反馈 只保留两天内的数据 其余的完结
    public function feedback_clear()
    {
        $time = time();
        $today0 = strtotime(date('Y-m-d', $time));
        $deadline = $today0 - 48 * 3600;
        $where = [];
        $where['ctime']=array('elt',$deadline);
        $update=M('user_feedback', '', 'KEFU')->where($where)->setField('status', 3);
        if($update){
            add_log('定时修改了反馈状态为完结');
        }
    }

    public function borrow(){
        $manager=I('admin_id');
        $month_s=1533052800;
        $month_end=1535731200;
        $borrow=array();//总借款数据
        $repay=array();//总还款数据
        $overdue=array();//总逾期数据
//        foreach ($manager as $k=>$v){
        $where=[];
        $where['a.type']=2;
        $where['a.admin_id']=$manager;
        $where['a.contact_status']=1;
        $where['b.get_time']=array('between',array($month_s,$month_end));
        $where['b.is_success']=2;
        $field='a.vid,a.upd_time,b.ctime as b_time,b.money,b.borrow_no';
        $user=M('kefu_ownuser','','KEFU')->join('a join dxd_caiwu.user_borrow b on b.vid=a.vid ')->where($where)->field($field)->select();

        $num=0;
        $p_arr=array();
        foreach ($user as $k=>$v){
            if($v['b_time']>$v['upd_time']){//借款时间在添加微信之后
                $borrow[]=array('borrow_no'=>$v['borrow_no'],'vid'=>$v['vid'],'money'=>$v['money']);//总的借款数据
                if(!in_array($v['vid'],$p_arr)){
                    $p_arr[]=$v['vid'];
                }
            }
        }

        $total_borrow=array_sum(array_column($borrow,'money'));
        $arr=array('借款人数'=>count($p_arr),'订单数'=>count($borrow),'总额'=>$total_borrow,'还款'=>149880,'逾期'=>23806);
        $a=array('沈阳'=>$arr);
        $arr=json_encode($a);
        var_dump($arr);
    }

    public function repayment(){
        $manager=I('admin_id');
        $month_s=1533052800;
        $month_end=1535731200;
        $borrow=array();//总借款数据
        $repay=array();//总还款数据
        $where=[];
        $where['a.type']=2;
        $where['a.admin_id']=$manager;
        $where['a.contact_status']=1;
        $where['b.repay_time']=array('between',array($month_s,$month_end));
        $where['b.status']=3;
        $field='a.vid,a.upd_time,b.repay_time,b.repay_money,b.borrow_no';
        $user=M('kefu_ownuser','','KEFU')->join('a join dxd_caiwu.user_repayment b on b.vid=a.vid ')->where($where)->field($field)->select();
        foreach ($user as $k=>$v){
            if($v['repay_time']>$v['upd_time']){//还款时间在添加微信之后
                $repay[]=array('borrow_no'=>$v['borrow_no'],'vid'=>$v['vid'],'money'=>$v['repay_money']);//总的借款数据
            }
        }
        $total_repament=array_sum(array_column($repay,'money'));
        var_dump($total_repament);
    }

    public function overdue()
    {
        $manager = I('admin_id');
        $month_s=1533052800;
        $month_end=1535731200;
        $overdue=array();//总逾期数据
        $where=[];
        $where['a.type']=2;
        $where['a.admin_id']=$manager;
        $where['a.contact_status']=1;
        $where['b.status']=1;
        $where['b.should_time']=array('between',array($month_s,$month_end));
        $field='a.vid,a.upd_time,b.should_time,b.repayment_money';
        $user=M('kefu_ownuser','','KEFU')->join('a join dxd_urge.user_overdue b on b.vid=a.vid ')->where($where)->field($field)->select();
        foreach ($user as $k=>$v){
            $overdue[]=array('repayment_money'=>$v['repayment_money'],'vid'=>$v['vid'],'money'=>$v['repayment_money']);//总的借款数据
        }
        $total_overdue=array_sum(array_column($overdue,'money'));
        var_dump($total_overdue);
    }

    //客户经理绩效
    public function user_manager(){
        $manager=I('admin_id');
        $month_s=1527782400;
        $month_end=1530374400;
        $borrow=array();//总借款数据
        $repay=array();//总还款数据
        $overdue=array();//总逾期数据
//        foreach ($manager as $k=>$v){
            $where=[];
            $where['a.type']=2;
            $where['a.admin_id']=$manager;
            $where['a.contact_status']=1;
            $where['b.ctime']=array('between',array($month_s,$month_end));
            $where['b.is_success']=2;
            $field='a.vid,a.upd_time,b.ctime as b_time,b.money,b.borrow_no';
            $user=M('kefu_ownuser','','KEFU')->join('a join dxd_caiwu.user_borrow b on b.vid=a.vid ')->where($where)->field($field)->select();
            foreach ($user as $k=>$v){
                if($v['b_time']>$v['upd_time']){//借款时间在添加微信之后
                    $borrow[]=array('borrow_no'=>$v['borrow_no'],'vid'=>$v['vid'],'money'=>$v['money']);//总的借款数据
                }
            }
            $total_borrow=array_sum(array_column($borrow),'money');


            $where=[];
            $where['a.type']=2;
            $where['a.admin_id']=$manager;
            $where['a.contact_status']=1;
            $where['b.repay_time']=array('between',array($month_s,$month_end));
            $where['b.status']=3;
            $field='a.vid,a.upd_time,b.repay_time,b.repay_money,b.borrow_no';
            $user=M('kefu_ownuser','','KEFU')->join('a join dxd_caiwu.user_repayment b on b.vid=a.vid ')->where($where)->field($field)->select();
            foreach ($user as $k=>$v){
                if($v['repay_time']>$v['upd_time']){//还款时间在添加微信之后
                    $repay[]=array('borrow_no'=>$v['borrow_no'],'vid'=>$v['vid'],'money'=>$v['repay_money']);//总的借款数据

                }
            }
            $total_repament=array_sum(array_column($repay),'money');


        $where=[];
        $where['a.type']=2;
        $where['a.admin_id']=$manager;
        $where['a.contact_status']=1;
        $where['b.status']=1;
        $where['b.ctime']=array('between',array($month_s,$month_end));
        $field='a.vid,a.upd_time,b.repay_time,b.repayment_money,b.borrow_no';
        $user=M('kefu_ownuser','','KEFU')->join('a join dxd_urge.user_overdue b on b.vid=a.vid ')->where($where)->field($field)->select();
        foreach ($user as $k=>$v){

                $overdue[]=array('borrow_no'=>$v['borrow_no'],'vid'=>$v['vid'],'money'=>$v['repay_money']);//总的借款数据

        }
        $total_overdue=array_sum(array_column($overdue),'repay_money');

    }

}